package com.app.acitivty;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;

import com.app.activity.R;
import com.app.adapter.ADlistAdpter;
import com.app.components.TopMenu;
import com.app.data.DataGeneration;
import com.app.data.InnItem;
import com.app.data.LineItem;
import com.app.data.ListDataModel;
import com.app.data.ScenicItem;
import com.app.data.TicketItem;
import com.app.data.YouhuiItem;
import com.app.service.HotelService;
import com.app.service.HttpCallBack;
import com.app.service.LineService;
import com.app.service.ScenicService;
import com.app.service.TicketService;
import com.app.service.adapter.HotelAdapter;
import com.app.service.adapter.LineAdapter;
import com.app.service.adapter.ScenicAdapter;
import com.app.utils.ActivityTools;
import com.app.utils.GlobalConstants;

public class HotActivity extends Activity implements HttpCallBack,OnItemClickListener {

	ADlistAdpter adpter;
	private LineItem[] lineitems=
		{
//			new LineItem(R.drawable.line1, "丽江泸沽湖旅游 泸沽湖纯玩 金沙江二日游 摩梭民俗花楼恋歌 ", "出发时间：2012-06-01", "￥455起"), 
//			new LineItem(R.drawable.line2, "云南丽江 泸沽湖 女儿国 婚俗游民俗特色三日游 体验走婚 纯玩团", "出发时间：2012-06-01", "￥718起"), 
//			new LineItem(R.drawable.line3, "云南丽江旅游 泸沽湖 走婚族 纯玩三日 猪糟船 花楼恋歌 摩梭特色", "出发时间：2012-06-01", "￥780起"), 
//			new LineItem(R.drawable.line4, "云南丽江旅游 玉龙雪山 牦牛坪一日游 含古维牦牛坪索道 印象丽江", "出发时间：2012-06-01", "￥455起"), 
//			new LineItem(R.drawable.line1, "丽江泸沽湖旅游 泸沽湖纯玩 金沙江二日游 摩梭民俗花楼恋歌 ","出发时间：2012-06-01", "￥455起"), 
//			new LineItem(R.drawable.line2, "云南丽江 泸沽湖 女儿国 婚俗游民俗特色三日游 体验走婚 纯玩团", "出发时间：2012-06-01", "￥718起"), 
//			new LineItem(R.drawable.line3, "云南丽江旅游 泸沽湖 走婚族 纯玩三日 猪糟船 花楼恋歌 摩梭特色", "出发时间：2012-06-01", "￥780起")
		};
	
	int currentIndex;
	private Button[] btns = new Button[4];
	private ArrayList<LineItem> lines;
	private ArrayList<InnItem> hotels;
	private ArrayList<ScenicItem> tickets;
	private ListView[] listviewList = new ListView[4];
	private boolean[] hasLoadDatas = {false,false,false,false};
	private ProgressDialog progressDialog;

	private void initView() {
		btns[0] = (Button) findViewById(R.id.btnPassengerTicket);
		btns[1] = (Button) findViewById(R.id.btnInn);
		btns[2] = (Button) findViewById(R.id.btnTickets);
		btns[3] = (Button) findViewById(R.id.btnLine);

		listviewList[0] = (ListView) findViewById(R.id.lvTicketList);
		listviewList[1] = (ListView) findViewById(R.id.lvInnList);
		listviewList[2] = (ListView) findViewById(R.id.lvScenicList);
		listviewList[3] = (ListView) findViewById(R.id.lvLineList);
		
		listviewList[0].setOnItemClickListener(
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View view,
							int postion, long arg3) {
						//ActivityTools.gotoActivity(HotActivity.this, TicketDetailActivity.class);
					}
		});
		
		listviewList[1].setOnItemClickListener(
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View view,
							int postion, long arg3) {
						
						InnItem item = hotels.get(postion);
						GlobalConstants.getInstance().setInnitem(item);
						ActivityTools.gotoActivity(HotActivity.this, InnDetailActivity.class);
					}
		});
		
		listviewList[2].setOnItemClickListener(
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View view,
							int postion, long arg3) {
						ScenicItem item = tickets.get(postion);
						GlobalConstants.getInstance().setScenicitem(item);
						ActivityTools.gotoActivity(HotActivity.this, ScenicDetailActivity.class);
					}
		});
		
		listviewList[3].setOnItemClickListener(
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View view,
							int postion, long arg3) {
						ActivityTools.gotoActivity(HotActivity.this, LineDetailActivity.class);
					}
		});
		
	}

	private ArrayList<HashMap<String, String>> mInnListItems = null;

	private String[] mInnColumnNames = { "innIcon", "innName", "innAddress",
			"innStarLevel", "innPrice" };

	private int[] mInnViewIds = { R.id.imgInnIcon, R.id.tvInnName,
			R.id.tvInnAddress, R.id.imgInnStarLevel, R.id.tvInnPrice };

	private ArrayList<HashMap<String, String>> mScenicItems = null;
	private String[] mScenicColumnNames = { "scenicIcon", "senicName",
			"senicPrice", "senicIntro", "senicSubject" };

	private int[] mScenicViewIds = { R.id.imgScenicIcon, R.id.tvSenicName,
			R.id.tvSenicPrice, R.id.tvSenicIntro, R.id.tvSenicSubject };

	private ArrayList<HashMap<String, String>> mItems = null;

	private String[] mTicketColumnNames = { "ticketTitle", "ticketPrice",
			"ticketStart", "ticketEnd" };

	private int[] mTicketViewIds = { R.id.tickt_item_title,
			R.id.tickt_item_price, R.id.tickt_item_start, R.id.tickt_item_end };

	private ArrayList<HashMap<String, String>> mLineListItems = null;

	private String[] mLineColumnNames = { "lineIcon", "lineName",
			"lineTransport", "linePrice" };

	private int[] mLineViewIds = { R.id.imgLineIcon, R.id.tvLineName,
			R.id.tvLineTransport, R.id.tvLinePrice };
	
	private boolean isLoadData[] = {false,false,false,false,false}; 

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hot);
		initView();

		TopMenu topMenu = new TopMenu(this, "返回", new OnClickListener() {

			@Override
			public void onClick(View v) {
				HotActivity.this.finish();
				
				//if (progressDialog != null) {
				progressDialog.dismiss();
				//}
				
			}
		}, R.drawable.top_button_unselect, R.drawable.top_button_selected, "热门");
		topMenu.onCreate();
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		int btnWidth = metric.widthPixels / 4;

		// 加入高亮显示效果
		for (int i = 0; i < btns.length; i++) {
			LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) btns[i]
					.getLayoutParams();
			linearParams.width = btnWidth;
			if (i!=0) {
				this.changeButtonState(i);
			}
			//this.changeButtonState(i);
		}
		//this.changeButtonState(2);
		listviewList[2].setVisibility(View.VISIBLE);

		// 机票列表
		mItems = new ArrayList<HashMap<String, String>>();

		for (int i = 0; i < 10; i++) {
			TicketItem ti = new TicketItem();
//			ti.setTicketTitle("中国国航VA1854");
//			ti.setTicketPrice("宁波的什么机场8点起飞");
//			ti.setTicketStart("丽江的什么机场10点到达");
//			ti.setTicketEnd("￥1033起");
			mItems.add(TicketListActivity.addItem(ti));
		}

		ListAdapter laTicket = new SimpleAdapter(this, mItems,
				R.layout.ticket_list_item, mTicketColumnNames, mTicketViewIds);
		listviewList[0].setAdapter(laTicket);

		// 客栈列表

		mInnListItems = new ArrayList<HashMap<String, String>>();
//		mInnListItems.add(InnListActivity.addItem(new InnItem("2418客栈", "丽江旅游 丽江住宿 2418客栈 怎人使馆/三多格门票 免费接机 古城", R.drawable.inn_01, R.drawable.star5, "￥538起")));
//		mInnListItems.add(InnListActivity.addItem(new InnItem("老房子客栈", "丽江旅游 丽江住宿 2418客栈 怎人使馆/三多格门票 免费接机 古城", R.drawable.inn_02, R.drawable.star5, "￥238起")));
//		mInnListItems.add(InnListActivity.addItem(new InnItem("希尔顿酒店", "丽江旅游 丽江住宿 2418客栈 怎人使馆/三多格门票 免费接机 古城", R.drawable.inn_03, R.drawable.star5, "￥534起")));
//		mInnListItems.add(InnListActivity.addItem(new InnItem("四季酒店", "丽江旅游 丽江住宿 2418客栈 怎人使馆/三多格门票 免费接机 古城", R.drawable.inn_04, R.drawable.star5, "￥390起")));
//  
		ListAdapter laInn = new SimpleAdapter(this, mInnListItems,
				R.layout.inn_list_item, mInnColumnNames, mInnViewIds);

		listviewList[1].setAdapter(laInn);

		// 景点列表

//		mScenicItems = new ArrayList<HashMap<String, String>>();
//
//		ScenicItem si = new ScenicItem();
//		si.setScenicIcon(R.drawable.s_01);
//		si.setSenicName("纳西古乐");
//		si.setSenicPrice("￥133起");
//		si.setSenicIntro("云南丽江旅游 古城 纳西古乐 演出门票 原生态纳西文化");
//		si.setSenicSubject("主题：生态旅游  休闲  生态旅游");
//		mScenicItems.add(ScenicSearchListActivity.addItem(si));
//
//		ScenicItem si1 = new ScenicItem();
//		si1.setScenicIcon(R.drawable.s_02);
//		si1.setSenicName("丽水金沙");
//		si1.setSenicPrice("￥783起");
//		si1.setSenicIntro("丽江旅游原生态 民族 歌舞表演 丽水加沙 演出门票");
//		si1.setSenicSubject("主题：生态旅游  休闲  生态旅游");
//		mScenicItems.add(ScenicSearchListActivity.addItem(si1));
//
//		ListAdapter lamScenic = new SimpleAdapter(this, mScenicItems,
//				R.layout.scenic_list_item, mScenicColumnNames, mScenicViewIds);
//
//		listviewList[2].setAdapter(lamScenic);

		// 套餐列表
		
		mLineListItems = new ArrayList<HashMap<String, String>>();

		for (int i = 0; i < 7; i++) {
//			LineItem li = lineitems[0];
//			li.setLineIcon(R.drawable.line_logo);
//			li.setLineName("上海到昆明 大理 丽江&lt;完美云南> 双飞六日游 HX3");
//			li.setLinePrice("￥2022起");
//			li.setLineTransport("出发时间:2012-06-02");
//			mLineListItems.add(LineListActivity.addItem(li));
		}

		ListAdapter laLine = new SimpleAdapter(this, mLineListItems,
				R.layout.line_list_item, mLineColumnNames, mLineViewIds);

		listviewList[3].setAdapter(laLine);
		
		showSection(1);
	}

	private void showSection (int section)
	{
		if(section == 0)
		{
			return;
		}
		if (isLoadData[section]) {
			return;
		}
		
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
			progressDialog.setTitle("提示");
			progressDialog.setMessage("读取数据中...");
			progressDialog.setCancelable(false);
			progressDialog.setIndeterminate(true);
		}
		progressDialog.show();
		if (section == 1) {
			HotelService service = new HotelService(this);
			service.execute("");
		}else if(section == 0) {
			TicketService service = new TicketService(this);
			service.execute("");
		}else if(section == 2) {
			ScenicService service = new ScenicService(this);
			service.execute("");
		}else if(section == 3) {
			LineService service = new LineService(this);
			service.execute("");
		}
	}
	
	private void changeButtonState(final int buttonid) {

		btns[buttonid].setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					for (int i = 0; i < btns.length; i++) {
						if (buttonid == i) {
							btns[buttonid].setBackgroundResource(R.drawable.tab_active);
							// 切换页面
							listviewList[i].setVisibility(View.VISIBLE);
							showSection(i);
						} else {
							btns[i].setBackgroundResource(R.drawable.tab_normal);
							listviewList[i].setVisibility(View.GONE);
						}
					}
				}
				return false;
			}

		});

	}

	@Override
	public void hotelFinishLoad(int code, ArrayList<InnItem> list) {
		System.out.println("hotelFinishLoad");
		currentIndex = 1;
		progressDialog.hide();
		hotels = list;
		HotelAdapter adapter = new HotelAdapter(this,list);
		listviewList[1].setAdapter(adapter);
		isLoadData[1] = true;
		
	}

	@Override
	public void lineFinishLoad(int code, ArrayList<LineItem> list) {
		progressDialog.hide();
		isLoadData[3] = true;
		progressDialog.dismiss();
		currentIndex =3;
		if (code == 0) {
			lines = list;
			LineAdapter adapter = new LineAdapter(this, list);
			listviewList[3].setAdapter(adapter);
			listviewList[3].setOnItemClickListener(this);
		}
	}

	@Override
	public void ticketFinishLoad(int code, ArrayList<ScenicItem> list) {
		System.out.println("ticketFinishLoad");
		progressDialog.hide();
//		ScenicItem si = new ScenicItem();
//		si.setScenicIcon(R.drawable.s_01);
//		si.setSenicName("纳西古乐");
//		si.setSenicPrice("￥133起");
//		si.setSenicIntro("云南丽江旅游 古城 纳西古乐 演出门票 原生态纳西文化");
//		si.setSenicSubject("主题：生态旅游  休闲  生态旅游");
//		
//		list.add(si);
		tickets = list;
		ScenicAdapter adapter = new ScenicAdapter(this, list);
		listviewList[2].setAdapter(adapter);
		isLoadData[2] = true;
		//progressDialog.dismiss();
	}

	@Override
	public void youhuiFinishLoad(int code, ArrayList<YouhuiItem> list) {
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		if (currentIndex == 3) {
			LineItem item = lines.get(position);
			GlobalConstants.getInstance().setLineitem(item);
			
			Intent intent = new Intent(this, LineDetailActivity.class);
			startActivity(intent);
		}
		
	}

//	@Override
//	public void ticketFinishLoad(int code, ArrayList<TicketItem> list) {
//		progressDialog.hide();
//		isLoadData[0] = true;
//		//progressDialog.dismiss();
//	}
}
